local loveTemplateFilename = "LoveTemplate.lua"

local debug = true

-- ===========================
-- Create a Love file used to playback the recorded session
-- ===========================
function LoveExport(directory, sessionName)

	local commands = {
		-- Create a directory.
		"mkdir " .. directory,
		
		-- Temporarily copy the love template as "main.lua"
		"copy " ..					
			loveTemplateFilename .. " " ..  -- Source filename
			"main.lua ",					-- Destination filename
		
		-- Temporarily copy the lua file into the current folder.	
		"copy " ..
			directory .. "\\" .. sessionName .. ".lua " .. -- Source
			sessionName .. ".lua ",							  -- Destination
			
		--"copy " ..
		--	"MapSizes.lua " ..
		--	"maps.lua ",
			
		
		-- Make the session's love file.
		"7z a -tzip " ..
			sessionName .. ".love " .. 					-- Destination filename 
			"main.lua " ..								-- file
			"conf.lua " ..
			"MapSizes.lua " ..							-- file
			"MEC.lua " ..								-- file
			sessionName .. ".lua " .. 					-- file
			"",	
		
		-- Move the Love file into the appropriate directory.
		"move " .. 
			".\\" .. sessionName .. ".love " ..		-- Source filename
			".\\" .. directory .. "\\" .. sessionName .. ".love ",	-- Destination
							
		-- Delete the temporary main.lua
		"del " .. ".\\main.lua ",
		
		-- Delete the temporary sessionName.lua file we copied.
		"del " .. sessionName .. ".lua ",
		
		---"del " .. "maps.lua ",
	}
	
	-- Create a batch file which launches the .love file with a console.
	if debug then
		local filename = directory .. "/" .. "Run" .. ".bat"
		local file = io.open(filename, "w")
		io.output(file)
		io.write("gnuplot " .. sessionName .. ".p\n")
		--io.write("love --console " .. sessionName .. ".love")
		file:close()
	end
	
	-- Run the commands sequentially.
	for i, command in ipairs(commands) do
		if debug then 
			print("===========================")
			print(command)
			print("===========================") 
		end
		
		os.execute(command)
		
		if debug then print() end
	end
end

-- Uncomment the following lines to be able to run this file independently (for testing purposes)
--[[
function LoveExportTest()
	debug = true
	local directory = "directory_name" 
	local sessionName = "session_name"
	LoveExport(directory, sessionName)
	
	print("io.popen(dir):lines()")
	for line in io.popen("dir"):lines() do
		local s = string.match(line, "%d%d%.%d%d%.%d%d@%d%d%-%d%d%-%d%d")
		if s then print(s) end
	end
end
 
LoveExportTest()
]]--